<!DOCTYPE html>
<html lang="zh-cmn-Hans">
<head>
    <meta charset="utf-8"/>
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=0"/>
    <title>本地缓存：设置默认值/设置缓存 ID/清除缓存/禁用缓存/启用缓存/缓存回调 - vditor</title>
    <meta name="robots" content="index,follow,archive">
    <meta name="description"
          content="Vditor 支持大纲、数学公式、脑图、图表、流程图、甘特图、时序图、五线谱、多媒体、语音阅读、标题锚点、代码高亮及复制、graphviz 渲染"/>
    <meta name="copyright" content="B3log"/>
    <link rel="canonical" href="https://vditor.b3log.org/demo/advanced-cache.html">
    <meta name="apple-mobile-web-app-capable" content="yes">
    <meta name="apple-mobile-web-app-status-bar-style" content="black">
    <meta http-equiv="Window-target" content="_top"/>
    <link rel="icon" type="image/png" href="//static.b3log.org/images/brand/vditor-128.png"/>
    <link rel="shortcut icon" type="image/x-icon" href="//static.b3log.org/images/brand/vditor-512.png"/>
    <link rel="apple-touch-icon" href="//static.b3log.org/images/brand/vditor-128.png">
    <link type="text/css" rel="stylesheet" href="//static.b3log.org/css/base.css" charset="utf-8"/>

    <meta property="og:locale" content="zh_CN"/>
    <meta property="og:title" content="本地缓存：设置默认值/设置缓存 ID/清除缓存/禁用缓存/启用缓存/缓存回调 - vditor"/>
    <meta property="og:description"
          content="Vditor 支持大纲、数学公式、脑图、图表、流程图、甘特图、时序图、五线谱、多媒体、语音阅读、标题锚点、代码高亮及复制、graphviz 渲染"/>
    <meta property="og:image" content="https://static.b3log.org/images/brand/vditor-128.png"/>
    <meta property="og:url" content="https://vditor.b3log.org"/>
    <meta property="og:site_name" content="B3log"/>
    <meta name="twitter:card" content="summary_large_image"/>
    <meta name="twitter:description"
          content="Vditor 支持大纲、数学公式、脑图、图表、流程图、甘特图、时序图、五线谱、多媒体、语音阅读、标题锚点、代码高亮及复制、graphviz 渲染"/>
    <meta name="twitter:title" content="本地缓存：设置默认值/设置缓存 ID/清除缓存/禁用缓存/启用缓存/缓存回调 - vditor"/>
    <meta name="twitter:image" content="https://static.b3log.org/images/brand/vditor-128.png"/>
    <meta name="twitter:url" content="https://vditor.b3log.org"/>
    <meta name="twitter:site" content="@b3logos"/>
    <meta name="twitter:creator" content="@b3logos"/>
    <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/vditor@latest/dist/index.css"/>
    <script src="//vditor.b3log.org/vditor.js?v1.0.0" defer></script>
    <style>
        .header {
            background-color: #fff;
            box-shadow: rgba(0, 0, 0, 0.05) 0 1px 7px;
            border-bottom: 1px solid #e1e4e8;
        }

        #log {
            margin-bottom: 10px;
            overflow: auto;
            height: 120px;
            border: 1px solid #eee;
            width: 100%;
            box-sizing: border-box;
            padding: 10px;
            border-radius: 3px;
        }
    </style>
</head>
<body>
<div class="header fn-clear">
    <a class="header-logo" href="https://vditor.b3log.org">
        <img src="//static.b3log.org/images/brand/vditor-128.png">
        <h1>Vditor</h1>
    </a>
    <div class="fn-clear">
        <a class="header-a" href="https://hacpai.com/tag/vditor" target="_blank">社区</a>
        <a class="header-a header-red" target="_blank" href="https://hacpai.com/article/1549638745630">API</a>
        <a class="header-a header-green current" href="index.html">Demo</a>
        <a class="header-a header-yellow" href="https://hacpai.com/sponsor" target="_blank">成为赞助者</a>
    </div>
</div>
<div class="wrapper">
    <div class="fn-50"></div>
    相关 <a href="https://hacpai.com/article/1549638745630#options-cache" target="_blank">API</a>：
    options.cache.enable，options.cache.id，options.cache.after()，<a
        href="https://hacpai.com/article/1549638745630#methods" target="_blank">disabledCache()</a>，<a
        href="https://hacpai.com/article/1549638745630#methods" target="_blank">enableCache()</a>，<a
        href="https://hacpai.com/article/1549638745630#methods" target="_blank">clearCache()</a>，<a
        href="https://hacpai.com/article/1549638745630#options" target="_blank">options.value</a><br>
    ⚠️ 注意事项：使用 innerHTML 和 options.value 设置初始值时需禁用缓存或清空缓存
    <div class="fn-50"></div>
    <div class="actions">
        <button class="btn btn--small" onclick="setCache(this, {value: '支持主窗口大小拖拽、字符计数', cache: {enable: false}});
setLog('')">
            设置初始值
        </button> &nbsp;
        <button class="btn btn--small" onclick="document.getElementById('vditor').innerHTML = '';
        setCache(this, {cache: {id:'vditorCache'}});
setLog('当前缓存 ID 为 vditorCache\n' + JSON.stringify(localStorage, null, '  '))">
            设置缓存 ID
        </button> &nbsp;
        <button class="btn btn--small" onclick="document.getElementById('vditor').innerHTML = '';
        setCache(this, {cache:{after(text){setLog('缓存回调，当前缓存 ID 为 vditorvditor\n' + JSON.stringify(localStorage, null, '  '))}}})">
            启用缓存回调
        </button> &nbsp;
        <button class="btn btn--small" onclick="vditor.clearCache();updateCode(this, 'vditor.clearCache()')
setLog('缓存已清除\n' + JSON.stringify(localStorage, null, '  '))">清除当前缓存
        </button> &nbsp;
        <button class="btn btn--small" onclick="vditor.disabledCache();;updateCode(this, 'vditor.disabledCache()')
setLog('缓存已禁用\n' + JSON.stringify(localStorage, null, '  '))">禁用当前缓存
        </button> &nbsp;
        <button class="btn btn--small" onclick="vditor.enableCache();;updateCode(this, 'vditor.enableCache()')
setLog('缓存已启用\n' + JSON.stringify(localStorage, null, '  '));">启用当前缓存
        </button> &nbsp;
    </div>
    <textarea id="log" readonly class="ft-fade">使用 innerHTML 做为初始值</textarea>
    <div id="vditor">
        <h2>Vditor</h2>
    </div>
    <div id="vditorDemoCode" class="vditor-reset">
        <pre><code class="javascript">// 使用 innerHTML 做为初始值
new Vditor('vditor', {
  cache: {
    enable: false,
  },
  height: 360,
})
// HTML
// &lt;div id="vditor">
//     &lt;h2>Vditor&lt;/h2>
// &lt;/div>
</code></pre>
    </div>
    <div class="fn-100"></div>
    <div class="fn-clear">
        <h2 class="fn-left">参与讨论</h2>
        <span class="fn-right" style="line-height: 36px" id="commentCnt"></span>
    </div>
    <div class="fn-50"></div>
    <div id="vditorComments"></div>
    <div class="fn-100"></div>
</div>
<!-- end main -->

<div class="footer">
    <div class="wrapper fn-clear">
        <a href="https://b3log.org">首页</a> &nbsp; &nbsp;
        <a href="https://hacpai.com" target="_blank">社区</a> &nbsp; &nbsp;
        <a href="https://b3log.org/brand-marking.html">品牌标识</a> &nbsp; &nbsp;
        <a href="https://hacpai.com/sponsor" target="_blank">成为赞助者</a>
        <div class="fn-right">
            © 2010-present <a href="https://b3log.org">B3log 开源</a>旗下云南链滴科技有限公司
        </div>
    </div>
</div>
<script>
  let vditor
  const setCache = (it, options) => {
    const mergedOptions = Object.assign({
      cache: {
        enable: false,
      },
      height: 360,
    }, options)

    if (it) {
      let code = JSON.stringify(mergedOptions, null, '  ')
      if (options.cache.after) {
        code = `{
  height: 360,
  cache:{
    after(text) {
      alert('缓存完毕回调，当前缓存值为' + text)
    }
  }
}`
      }
      updateCode(it, `new Vditor('vditor', ${code})`)
    }
    vditor = new Vditor('vditor', mergedOptions)
  }
  vditorScript = setCache
  const setLog = (text) => {
    document.getElementById('log').textContent = text
  }
</script>
</body>
</html>
